package com.yanqu.road.dao.impl.activity.douluoshrek;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.douluoshrek.*;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DouLuoShrekConfigDaoImpl extends TempDao {

    public static String getSkillCommentSql = "select * from t_s_activity_college_skill where activityId= ?";
    public Map<Integer, Map<Integer,SkillConfig>> getSkillMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer,SkillConfig>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSkillCommentSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SkillConfig data = new SkillConfig();
                data.setType(rs.getInt("type"));
                data.setLevel(rs.getInt("level"));
                data.setIcon(rs.getInt("icon"));
                data.setName(rs.getString("name"));
                data.setDesc(rs.getString("desc"));
                data.setCost(rs.getInt("cost"));
                data.setSkillEffect(rs.getString("skillEffect"));
                data.setWeight(rs.getInt("weight"));
                if(!result.containsKey(data.getType())){
                    result.put(data.getType(),new ConcurrentHashMap<>());
                }
                result.get(data.getType()).put(data.getLevel(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String getCounterSql = "select * from t_s_activity_college_counter where activityId= ?";
    public Map<Integer, Map<Integer,CounterConfig>> getCounterMap(int activityId){
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer,CounterConfig>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getCounterSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CounterConfig data = new CounterConfig();
                data.setType(rs.getInt("type"));
                data.setLevel(rs.getInt("level"));
                data.setNum(rs.getInt("num"));
                data.setConsume(rs.getInt("consume"));
                if(!result.containsKey(data.getType())){
                    result.put(data.getType(),new ConcurrentHashMap<>());
                }
                result.get(data.getType()).put(data.getLevel(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }


    public static String getTitleSql = "select * from t_s_activity_college_title where activityId= ?";
    public Map<Integer, TitleConfig> getTitleMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,TitleConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getTitleSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                TitleConfig data = new TitleConfig();
                data.setId(rs.getInt("id"));
                data.setIcon(rs.getInt("icon"));
                data.setName(rs.getString("name"));
                data.setDesc(rs.getString("desc"));
                data.setSourPower(rs.getString("sourPower"));
                data.setRewards(rs.getString("rewards"));
                data.setAccelerateTimeCost(rs.getInt("accelerateTimeCost"));
                result.put(data.getId(),data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String getBeastSql = "select * from t_s_activity_college_beast where activityId= ?";
    public Map<Integer, BeastConfig> getBeastMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, BeastConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getBeastSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BeastConfig data = new BeastConfig();
                data.setId(rs.getInt("id"));
                data.setIcon(rs.getInt("icon"));
                data.setName(rs.getString("name"));
                data.setDesc(rs.getString("desc"));
                data.setSourPower(rs.getString("sourPower"));
                data.setRewards(rs.getString("rewards"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String getEventSql = "select * from t_s_activity_college_event where activityId= ?";
    public Map<Integer, EventConfig> getEventMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, EventConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getEventSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                EventConfig data = new EventConfig();
                data.setId(rs.getInt("id"));
                data.setIcon(rs.getString("icon"));
                data.setRewards(rs.getString("rewards"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }
}
